export default{
	namespaced:true,
	state:{
		userInfo:JSON.parse(uni.getStorageSync('userInfo')||'{}'),
		authToken:uni.getStorageSync('authToken')||''
	},
	mutations:{
		SET_AUTH_TOKEN(state,payload){
			state.authToken=payload
			uni.setStorageSync('authToken',payload)
			
		},
		SET_USER_INFO(state,payload){
			state.userInfo=payload
			uni.setStorageSync('userInfo',JSON.stringify(payload))
		},
		REMOVE_USER(state,payload){
			uni.removeStorageSync("userInfo")
			uni.removeStorageSync("authToken")
			state.userInfo={}
			state.authToken=''
		}
	},
	actions:{
		async getUserInfoByUidAsync(cxt,uid){
			console.log('-------获取会员信息的方法-----------');
			console.log('uid',uid);
			const result=await uni.$api.mineModules.getUserInfoByUid(uid)
			console.log('会员信息',result.data.data);
			cxt.commit('SET_USER_INFO',result.data.data)
			uni.switchTab({
				url:'/pages/mine/mine'
			})
		},
		async loginAsync(cxt,payload){
			console.log('-------登录的异步方法-----------');
			console.log('payload',payload);
			//调用登录的api方法
			const result=await uni.$api.mineModules.accountLogin(payload)
			console.log('result',result);
			if(result.data.code==200){
				const userInfo=result.data.data
				const authToken=result.data.data.auth_token
				cxt.commit('SET_AUTH_TOKEN',authToken)
				console.log('userInfo',userInfo.uid);
				cxt.dispatch('getUserInfoByUidAsync',userInfo.uid)
			}
		},
		/**
		 * 安全退出
		 */
		async safeLogoutAsync(cxt,payload){
			console.log('---------安全退出的异步方法-------------------');
			console.log('payload',payload);
			const result=await uni.$api.mineModules.safeLogoutApi(payload)
			if(result.data.code==200){
				cxt.commit('REMOVE_USER')
			}
		}
	}
}